#!/usr/bin/python
#-*-coding: utf-8 -*-

########## DO NOT CHANGE THESE LINES ###########################################
import sys
from pylot.core import (ENCODING, Path)
try : __file__
except NameError : co = sys._getframe(0).f_code ; __file__ = co.co_filename
ufile = __file__.decode(ENCODING)
if ufile.strip() : PLUGIN_PATH = Path(ufile).abspath().parent
else : PLUGIN_PATH = Path(u'.').abspath()
########## DO NOT CHANGE THESE LINES ###########################################

from PyQt4 import QtGui
from PyQt4.QtCore import SIGNAL

from handler import HandlerPart
from pylot.qtgui import (load_designer_file, GuiAutoWidget)
_GuiPart, WidgetClass = load_designer_file(u'designer.ui', [PLUGIN_PATH])
class GuiPart(WidgetClass, GuiAutoWidget, _GuiPart):
  u"""
  A very simple divider
  """
  def __init__(self, handler=None):
    if handler is None :
      handler = HandlerPart()
      handler.init()
    GuiAutoWidget.__init__(self, handler)
    self._plugin_path = PLUGIN_PATH

    WidgetClass.__init__(self)
    self.setupUi(self) # Designer file initialization

    # Widget creation
    self._createActions()
    self._createConnexions()
    self._createMenu()
    self._createToolBars()

  # ============================================================================
  # Widget Creation
  # ============================================================================

  def _createActions(self):
    u"""
    See 'designer.ui' file (Qt Designer file)
    """
    pass

  def _createConnexions(self):
    # Computing actions
    self.connect(self.action_example, SIGNAL('triggered()'),
                 self._onActionExampleTriggered)
    self.connect(self.pb_run, SIGNAL('clicked()'),
                 self._onPbRunClicked)

  def _createMenu(self):
    self.menu_example = QtGui.QMenu()
    self.menu_example.setObjectName(u'menu_example')
    self.addMenu_(self.menu_example)

  def _createToolBars(self):
    self.tool_bar_example = QtGui.QToolBar()
    self.tool_bar_example.setObjectName(u'tool_bar_example')
    self.addToolBar_(self.tool_bar_example)

#  def _createPreferences(self):
#    self.pref = self.gui_processhandler.pref

  # ============================================================================
  # Widget dynamic changes
  # ============================================================================

  def _fillMenus(self, level):
    self._fill(self.menu_example, [
      self.action_example,
      ])

  def _fillToolBars(self, level):
    self._fill(self.tool_bar_example, [
      self.action_example,
      ])

  def _decorate (self):
    self.action_example.setIcon(self._icon(u'example.png'))

  def _translate(self):
    self._setTexts([
      (self.menu_example, self.tr_(u'Example')),
      (self.tool_bar_example, self.tr_(u'Example ToolBar'))
      ])

  # ============================================================================
  # Widget/HandlerPart Connections
  # ============================================================================

  def _onActionExampleTriggered(self):
    self.handler.example_method()

  def _onPbRunClicked(self):
    cmd = unicode(self.line_cmd.text())
    self.handler.process.runCmd(cmd)

  # ============================================================================
  # Widget Public Methods
  # ============================================================================

if __name__ == u"__main__":
  from PyQt4.QtGui import QApplication
  app = QApplication([])
  gui = GuiPart()
  gui.show()
  ret = app.exec_()
